//
//  LBCTopDataTool.m
//  LifeEnjoy
//
//  Created by 刘表聪 on 15/9/10.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "LBCTopDataTool.h"
#import "AFNetworking.h"
#import "LBCTopNews.h"


@interface LBCTopDataTool ()

// 首页头部视图的轮播图模型数组
@property (nonatomic, strong) NSMutableArray *topImageNewsArray;
// 首页头部视图的轮播图Url数组
@property (nonatomic, strong) NSMutableArray *topImageUrls;

// 首页头部视图的按钮模型数组
@property (nonatomic, strong) NSMutableArray *topButtonNewsArray;


@end

@implementation LBCTopDataTool
singleton_implementation(LBCTopDataTool)

#pragma mark - 初始化方法
- (instancetype)init
{
    self = [super init];
    if (self) {
        // 加载网络数据s
        [self loadTopData];
    }
    return self;
}

#pragma mark - 头部数据网络请求
- (void)loadTopData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", nil];;
    
    __block typeof (self) weakSelf = self;
    [manager GET:kHomeTopUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dataDict = [responseObject[@"data"] firstObject];
        // 首页头部视图的按钮模型字典
        for (NSDictionary *dict in dataDict[@"fnAreas"]) {
            
            if ([dict.allValues containsObject:@"我的收藏"] || [dict.allValues containsObject:@"美妆视频"] || [dict.allValues containsObject:@"精华榜"] || [dict.allValues containsObject:@"膜么哒"]) {
                LBCTopNews *topNews = [LBCTopNews new];
                for (NSString *key in dict.allKeys) {
                    
                    
                    NSString *value = dict[key];
                    
                    [topNews setValue:value forKey:key];
                    
                    
                }
                [weakSelf.topButtonNewsArray addObject:topNews];
            }
           
        }
        
        // 首页头部视图的轮播图模型字典
        for (NSDictionary *dict in dataDict[@"foucs"]) {
            LBCTopNews *topNews = [LBCTopNews new];
            [topNews setValuesForKeysWithDictionary:dict];
            // 模型数组
            [weakSelf.topImageNewsArray addObject:topNews];
            // Url数组
            [weakSelf.topImageUrls addObject:topNews.imgUrl];
            weakSelf.scrollImageUrls = [NSArray arrayWithArray:_topImageUrls];
        }
        
        if (_updateTopBlock) {
            _updateTopBlock();
        } else {
            LBCLog(@"UpdateTopBlcok is null");
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if (error) {
            LBCLog(@"%@", error);
        }
        
    }];
}

#pragma mark - 根据下标得到具体按钮数据
- (LBCTopNews *)topNewsAtIndex:(NSInteger)index
{
    return self.topButtonNewsArray[index];
}

#pragma mark 根据下标得到具体轮播图数据
- (LBCTopNews *)topImageNewsAtIndex:(NSInteger)index
{
    return self.topImageNewsArray[index];
}



#pragma mark - 重写方法
- (NSInteger)topImageCount
{
    return self.topImageNewsArray.count;
}

- (NSInteger)topButtonCount
{
    return self.topButtonNewsArray.count;
}

#pragma mark - 懒加载
- (NSMutableArray *)topImageNewsArray
{
    if (!_topImageNewsArray) {
        self.topImageNewsArray = [NSMutableArray array];
    }
    return _topImageNewsArray;
}

- (NSMutableArray *)topButtonNewsArray
{
    if (!_topButtonNewsArray) {
        self.topButtonNewsArray = [NSMutableArray array];
    }
    return _topButtonNewsArray;
}

- (NSMutableArray *)topImageUrls
{
    if (!_topImageUrls) {
        self.topImageUrls = [NSMutableArray array];
    }
    return _topImageUrls;
}

@end






